/* Custom jquery function */
$.fn.flowUp = function(options) {
    var step = options["step"];
    var duration = options["duration"];
    var callback = options["callback"];

    $(this).animate({
        "position":"absolute",
        "top": "-=" + $(this).height() * step + "px"
    },
    duration,
    callback
    );
}
$.fn.flowLeft = function(options) {
    var step = options["step"];
    var duration = options["duration"];
    var callback = options["callback"];

    $(this).animate({
        "position":"absolute",
        "right": "+=" + $(this).width() * step + "px"
    },
    duration,
    callback
    );
}
$.fn.flowDown = function(options) {
    var step = options["step"];
    var duration = options["duration"];
    var callback = options["callback"];

    $(this).animate({
        "position":"absolute",
        "top": "+=" + $(this).height() * step + "px"
    },
    duration,
    callback
    );
}
$.fn.flowRight = function(options) {
    var step = options["step"];
    var duration = options["duration"];
    var callback = options["callback"];

    $(this).animate({
        "position":"absolute",
        "right": "-=" + $(this).width() * step + "px"
    },
    duration,
    callback
    );
}
/* Control function */
var CHILD_FD_PANELS = new Array();
var FD_CONTROL_BUTTONS = new Array();
var FD_PANEL_CONTROL = 1;
var CATEGORY_CONTROL = 2;
var LANGUAGE_CONTROL = 3;
$.fn.control = function(options){
    var type = options["type"];
    if(type == FD_PANEL_CONTROL){
        var panel = options["panel"];
        CHILD_FD_PANELS.push(panel);
        FD_CONTROL_BUTTONS.push(this);
        $(this).click(function(){
            if ($(panel).parent().is(':hidden') || (!$(panel).parent().is(':hidden') && $(panel).is(':hidden'))){
                for (var i=0; i < CHILD_FD_PANELS.length; i++) {
                    $(CHILD_FD_PANELS[i]).hide();
                }
                for (i=0; i < FD_CONTROL_BUTTONS.length; i++) {
                    $(FD_CONTROL_BUTTONS[i]).removeClass("active");
                }
                $(panel).show();
                $(this).addClass("active");
                if($(panel).parent().is(':hidden')){
                    $(panel).parent().slideToggle("fast");
                }
            }else if (!$(panel).parent().is(':hidden') && !$(panel).is(':hidden')){
                $(panel).parent().slideToggle("fast");
                for (i=0; i < CHILD_FD_PANELS.length; i++) {
                    $(CHILD_FD_PANELS[i]).hide();
                }
                $(this).removeClass("active");
            }
        });
    } else if(type == CATEGORY_CONTROL){
        $(this).click(function() {
            var subNav = options["subNav"];
            var width = $(this).width();
            $(subNav).css({
                width:width + "px",
                position: "absolute",
                bottom: $(subNav).parent().height() + 2 + "px"
            });
            $(subNav).slideToggle("normal");
        });
    } else if(type == LANGUAGE_CONTROL){
        var inputs = $(this);    
        $.each(inputs, function() {
            var pathName = $(this).attr('alt');
            $(this).click(function(){
                window.location.href = getDirectUrl(pathName);
            });
        });
    }
}
/*Expand & Collapse Panel*/
$.fn.expandCollapsePanel = function(options) {
    var button = options["button"];
    var container = options["containerPanel"];
    var content = options["contentPanel"];
    var config = options["config"];
    
    $(button).click(function() {
        if(!$(this).hasClass('active')){
            $(container).animate({
                height: $(content).height()
            },function(){
                $(container).removeClass('doc-description-collapsed');
                $(button).addClass('active');
            });
        }else{
            $(container).addClass('doc-description-collapsed');
            $(container).animate({
                height: config.height + "px"
            },function(){
                $(button).removeClass('active');
            });
        }
    });
}

/* getDirectUrl */
function getDirectUrl(pathName){
    return window.location.protocol + "//" + window.location.host + pathName;
}
/* Login Panel */
$.fn.login = function(options) {
    var loginUrl = options["loginUrl"];
    var email = options["email"];
    var password = options["password"];
    
    $.ajax({
        url: loginUrl,
        type: "POST",
        dataType: "json",
        data: {
            email: email,
            password: password
        },
        success: function (responseJson) {
            if(responseJson == null){
                location.reload();
            }
            alert(responseJson);
        }
    });
}
